package com.tender.desensitization.factory.instance;

import java.util.Map;

public class MapProcessFactory implements TypeProcessFactory {

    private TypeProcessFactoryFacade factoryFacade;

    public MapProcessFactory(TypeProcessFactoryFacade factoryFacade) {
        this.factoryFacade = factoryFacade;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return Map.class.isAssignableFrom(clazz);
    }

    @Override
    public Object processType(Object instance) {
        if (null == instance) {
            return null;
        }
        Map map = (Map) instance;
        for (Object obj : map.entrySet()) {
            Map.Entry entry = (Map.Entry) obj;
            Object value = entry.getValue();
            if (null != value) {
                TypeProcessFactory processFactory = factoryFacade.getProcessFactory(value.getClass());
                if (null != processFactory) {
                    processFactory.processType(value);
                }
            }
        }

        return instance;
    }
}